<template>

    <div class="toady-news" :class="{ 'is-mobile': $isMobile }">
        <div class="toady-news-title">
            <div>新闻动态</div>
            <span @click="handleNewMore">查看更多</span>
        </div>
        <div class="toady-news-list">
            <div v-for="( item, index ) in  list" :key="item.id" class="news-item" @click="handleDetail(item)">
                <div class="news-item-index" :class="[`news-item-index${index + 1}`]">
                    {{ index + 1 }}
                </div>
                <div class="news-item-content">
                    <div class="news-item-title">{{ item.title }}</div>
                    <div class="news-item-publishTime">
                        {{ formatTime(item.publishTime) }}
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            pageNum: 1,
            pageSize: 6,
            id: 25,    // 新闻动态-历届商业企业家活动日回顾
            list: []
        }
    },

    mounted() {
        this.init()
    },

    methods: {

        async init() {
            const { pageNum, pageSize } = this;
            const {
                data: { sorts },
            } = await this.$fetch.get(
                `/api/directory-article/list`,
                {
                    directoryId: this.id,
                    pageNum,
                    pageSize,
                },
            );
            this.list = sorts.map((item) => {
                const { article, ...others } = item;
                return {
                    ...others,
                    ...article,
                };
            });
        },

        handleDetail(item) {
            this.$router.push({
                path: `/article-detail/${item.articleId}`,
            });
        },
        handleNewMore() {
            this.$router.push({
                path: `/article-list`,
                query: {
                    id: this.id,
                },
            });
        },

        formatTime(time) {
            if (!time) {
                return "";
            }
            const date = new Date(time);
            const year = date.getFullYear();
            const month = `${date.getMonth() + 1}`.slice(-2); // 补零操作
            const day = `${date.getDate()}`.slice(-2); // 补零操作

            return `${year}年 . ${month}月${day}日`;
        },

    }
}
</script>

<style lang="less" scoped>
.toady-news {
    padding: 20px 25px;
    background: #fff;
    width: 33.3%;

    .toady-news-title {
        height: 38px;
        display: flex;
        justify-content: space-between;
        padding-bottom: 12px;
        border-bottom: 1px solid #efefef;
        margin-bottom: 21px;

        div {
            font-family: PingFangSC, PingFang SC;
            font-weight: 400;
            font-size: 24px;
            color: #333333;
            line-height: 36px;
            text-align: left;
            font-style: normal;
            text-transform: none;
        }

        span {
            font-family: PingFangSC, PingFang SC;
            font-weight: 400;
            font-size: 16px;
            color: #3268ae;
            line-height: 22px;
            text-align: left;
            font-style: normal;
            text-transform: none;
            cursor: pointer;
        }
    }

    .toady-news-list {
        margin-top: 24px;

        .news-item {
            margin-bottom: 20px;
            display: flex;
            width: 100%;
            flex-direction: row;

            .news-item-index {
                width: 20px;
                height: 20px;
                margin-right: 10px;
                background: #cccccc;
                display: flex;
                align-items: center;
                justify-content: center;
                color: #fff;

                &.news-item-index1 {
                    background: #e8400c;
                }

                &.news-item-index2 {
                    background: #f88215;
                }

                &.news-item-index3 {
                    background: #ffb910;
                }
            }

            .news-item-content {
                flex: 1;
                display: flex;
                flex-direction: column;
                cursor: pointer;

                &:hover {

                    .news-item-title,
                    .news-item-publishTime {
                        color: #3560ab;
                    }
                }

                .news-item-title {
                    font-family: PingFangSC, PingFang SC;
                    font-weight: 400;
                    font-size: 16px;
                    color: #484848;
                    line-height: 22px;
                    text-align: left;
                    font-style: normal;
                    text-transform: none;
                    margin-bottom: 6px;
                }

                .news-item-publishTime {
                    font-family: PingFangSC, PingFang SC;
                    font-weight: 400;
                    font-size: 14px;
                    color: #aeaeae;
                    line-height: 20px;
                    text-align: left;
                    font-style: normal;
                    text-transform: none;
                }
            }
        }
    }
}

.toady-news.is-mobile {
    width: 100%;
    padding: 10px;

    .toady-news-list {
        margin-top: 0;

        .news-item {
            margin-bottom: 10px;
        }
    }

    .toady-news-title {
        font-size: 20px;

        div {
            font-size: 20px;
        }
    }

    .news-item-publishTime {
        display: none;
    }

    .news-item-title {
        text-decoration: none;
    }
}
</style>